/**
 * 通用输送线 - 输送线.
 */
export default {
  onParameterUpdate: function (self, ctx, key, value) {
    const collisions = self.getParameter('__collision__') || [];

    // 应力变化
    if (key === 'pForce') {
      collisions.forEach(object => this.doForceUpdate(self, object));
    }

    // 启用
    if (key === 'pEnabled' && (
      value === true ||
      value === 1 ||
      value === '1'
    )) {
      collisions.forEach(object => this.doForceUpdate(self, object));
    }

    // 禁用
    if (key === 'pEnabled' && (
      value === false ||
      value === 0 ||
      value === '0'
    )) {
      collisions.forEach(object => this.doForceClear(self, object));
    }
  },
  onCollisionStart: function (self, object) {
    this.doForceUpdate(self, object);
  },
  onCollisionEnd: function (self, object) {
      console.log(self);
    if(object) this.doForceClear(self, object);
  },
  doForceUpdate: function (self, object) {
    const UUID = self.uuid;
    const FORCE = 'pForce';
    const PRIORITY = 'pPriority';
    const ENABLED = 'pEnabled';

    // 跳过禁用设备
    const enabled = self.getParameter(ENABLED);
    if (enabled === false || enabled === 0 || enabled === '0') {
      return;
    }

    // 获取应力及优先级参数
    const vector = this.vector3(self.getParameter(FORCE) || '');
    const priority = parseInt(self.getParameter(PRIORITY)) || 0;

    // 更新碰撞对象应力列表
    const force = object.getParameter(FORCE) || {};
    force[UUID] = {
      vector: vector,
      priority: priority,
    };
    object.setParameter(FORCE, force, true);
  },
  doForceClear: function (self, object) {
    const UUID = self.uuid;
    const FORCE = 'pForce';
    const force = object.getParameter(FORCE) || {};
    delete force[UUID];
    object.setParameter(FORCE, force, true);
  },
  vector3: function (str) {
    let components = str.substr(1, str.length - 2).split(',');
    return {
      x: (components[0] || '').trim(),
      y: (components[1] || '').trim(),
      z: (components[2] || '').trim(),
    }
  },
}